<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>龟兔赛跑第一遍</title>
    <style>
        #box1{
            width: 0;
            height: 1000px;
            position: absolute;
            border-left: 1px black solid;
            top: 0;
            left: 999px;
        }
        #box2{
            width: 100px;
            height: 100px;
            background:url("../imagconman/rabbit.gif");
            background-size: 100px;
            position: absolute;
            left: 0;
        }
        #box3{
            width: 100px;
            height: 100px;
            background:url("../imagconman/tortoise.gif") no-repeat;
            background-size: 100px;
            position: absolute;
            left: 0;
            top:200px
        }
        #box4{
            width: 100px;
            height: 100px;
            background-color: green;
            position: absolute;
            left: 0;
            top:330px
        }
    </style>
    <script>
        window.onload=function () {
            var btn01=document.getElementById("btn01");
            var btn02=document.getElementById("btn02");
            var btn03=document.getElementById("btn03");
            var btn04=document.getElementById("btn04");
            var btn05=document.getElementById("btn05");
            var box2=document.getElementById("box2");
            var box3=document.getElementById("box3");
            var box4=document.getElementById("box4");
            btn01.onclick=function () {
                move(box2,"left",999,10);
            };
            btn02.onclick=function () {
                move(box3,"left",999,10);
            };
            btn03.onclick=function () {
                move(box2,"left",0,10);
            };
            btn04.onclick=function () {
                move(box3,"left",0,10)
            };
            btn05.onclick=function () {
                move(box4,"width",999,10,function () {
                    move(box4,"height",300,10,function () {
                        move(box4,"width",100,10,function () {
                            move(box4,"height",100,10,function () {

                            });
                        });
                    });
                });
            };
        };
        function move(obj,attr,target,speed,callback) {
            var current=parseInt(getStyle(obj,attr));
            if (current>target){
                speed=-speed;
            }
            //点击按钮以后box1向右移动
            clearInterval(obj.timer);
            obj.timer=setInterval(function () {
                var oldValue=parseInt(getStyle(obj,attr));
                var newValue=oldValue+speed;
                //向右 移动0-800 需要判断newValue是否大于target
                //向左 移动800-0 需要判断newValue是否小于target
                if ((speed>0&&newValue>target) ||(speed<0&&newValue<target)){
                    newValue=target;
                }
                obj.style[attr]=newValue+"px";
                if (newValue==target){
                    clearInterval(obj.timer);
                    callback&&callback();
                }
            },50);
        }
        function getStyle(obj,name) {
            if (window.getComputedStyle){
                return getComputedStyle(obj,null)[name];
            }else {
                return obj.currentStyle[name];
            }
        }
    </script>
</head>
<body>
<button id="btn01">兔子开始跑</button>
<button id="btn02">乌龟开始跑</button>
<button id="btn03">兔子回起点</button>
<button id="btn04">乌龟回起点</button>
<button id="btn05">div测试</button>
<div id="box1"></div>
<div id="box2"></div>
<div id="box3"></div>
<div id="box4"></div>
</body>
</html>